package com.lmscn.lms.api.interceptor;

import com.jfinal.aop.Invocation;
import com.jfinal.aop.PrototypeInterceptor;
import com.lmscn.lms.api.controller.CourseBaseController;
import java.util.List;

/**
 * Created by Czl on 2015/8/6.
 */
public class KeyCondition extends PrototypeInterceptor {

    private String queryString;
    private CourseBaseController cbc;

    /**
     * 对Condition的属性进行赋值初始化
     */
    private void init(Invocation ai) {
        cbc = (CourseBaseController)ai.getController();
        queryString = cbc.getPara("queryString");
    }

    @Override
    public void doIntercept(Invocation inv) {
        init(inv);
        cbc.kcd = this;
        inv.invoke();
    }

    /**
     * 生成CourseBaseController的查询条件
     */
    public void buildSql(StringBuilder sql, List<Object> paras) {
        sql.append("from t_course_base t where 1=1 ");
        sql.append(" and name like ? ");
        paras.add("%" + queryString + "%");
        // 由老师名字更换为关键词 
        sql.append(" or tags like ?");
        paras.add("%" + queryString + "%");
    }

}
